/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: directory.cpp
 *
 * Purpose: directory wrapper for msvc implementation
 *
 * Developer:
 *   wen.gu , 2019-10-15
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/

#if defined(_MSC_VER)
#include "win_render.h"
#include <chrono>
#include <thread>     
#include <Windows.h>
#include <io.h>
#include <direct.h> 



/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
WinRender::WinRender()
{
    if(SDL_Init(SDL_INIT_VIDEO)) {  
		printf( "Could not initialize SDL - %s\n", SDL_GetError()); 
	} 
}

WinRender::~WinRender()
{
    if (render_texture_)
    {
        SDL_DestroyTexture(render_texture_);
    }

    if (render_)
    {
        //SDL_DestroyRender(render_);
    }

    if (display_win_)
    {
        SDL_DestroyWindow(display_win_);
    }
}

bool WinRender::initialize(const render_param& param)
{
    img_width_ = param.img_width;
    img_height_ = param.img_height;
    display_win_ = SDL_CreateWindow("kfa player", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
		                            param.img_width, param.img_height, SDL_WINDOW_OPENGL|SDL_WINDOW_RESIZABLE);
    
    render_ = SDL_CreateRenderer(display_win_, -1, 0);

    render_texture_ = SDL_CreateTexture(render_, SDL_PIXELFORMAT_NV12, SDL_TEXTUREACCESS_STREAMING, param.img_width, param.img_height);

    render_rect_.x = 0;  
    render_rect_.y = 0;  
    render_rect_.w = param.img_width;  
    render_rect_.h = param.img_height; 

    return display_win_ && render_ && render_texture_;
}

bool WinRender::render(const KfaBuffer& buffer)
{
    SDL_UpdateTexture(render_texture_, NULL, buffer.buf, img_width_);

    SDL_RenderClear(render_);   
    SDL_RenderCopy(render_, render_texture_, NULL, &render_rect_);  
    SDL_RenderPresent( render_ );

    return true;
}

#endif /** defined _MSC_VER */
